package com.frxuexi.park.view

import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.ParkBean
import com.frxuexi.lib_base.entity.StatusBean
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.http.RequestType
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.room.ParkEntity
import com.frxuexi.lib_base.utils.DateTimeUtils
import com.frxuexi.park.R
import com.frxuexi.park.adapter.PreDateAdapter
import com.frxuexi.park.intent.ParkPreIntent
import com.frxuexi.park.viewmodel.ParkPreViewModel
import com.frxuexi.park.databinding.ActivityParkPreBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import java.util.Calendar
import java.util.Date

@Route(path = "/park/ParkPreActivity")
@AndroidEntryPoint
class ParkPreActivity : BaseActivity<ActivityParkPreBinding, ParkPreIntent, ParkPreViewModel>() {
    override val binding: ActivityParkPreBinding by lazy {
        ActivityParkPreBinding.inflate(
            layoutInflater
        )
    }
    override val vm: ParkPreViewModel by viewModels<ParkPreViewModel>()
    val preDateAdapter by lazy { PreDateAdapter() }
    @JvmField
    @Autowired
    var id:Int?=-1
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when (it.type) {
                            RequestType.DEFAULT -> {
                                val num = it.data as Int
                                binding.parkpreNum.setText("${100 - num}")
                            }
                            RequestType.ADD->{
                                ToastUtils.showLong("预约成功")
                                finish()
                            }
                            RequestType.SELECT_ITEM->{
                                val entity = it.data as ParkEntity
                                preDateAdapter.data.forEach {
                                    if(DateTimeUtils.getDate(it.content as Date)==entity.date){
                                        it.checked=true
                                    }else{
                                        it.checked=false
                                    }
                                }
                                preDateAdapter.notifyDataSetChanged()

                                when(entity.time){
                                    "上午"-> binding.parkpreRg.check(R.id.parkpre_morning)
                                    "下午"-> binding.parkpreRg.check(R.id.parkpre_after)
                                    "晚上"-> binding.parkpreRg.check(R.id.parkpre_evening)
                                }

                                binding.parkpreCarNo.setText(entity.parkNum)
                                binding.parkpreCarNum.setText(entity.carNum)
                            }

                            RequestType.UPDATE->{
                                ToastUtils.showLong("修改成功")
                                finish()
                            }

                            else -> {}
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)

        vm.sendIntent(ParkPreIntent.getCountByDate(DateTimeUtils.getDate(System.currentTimeMillis())))

        if(id!=-1){
            vm.sendIntent(ParkPreIntent.getParksById(id!!))
        }
        val list = mutableListOf<StatusBean>()
        var calendar = Calendar.getInstance()
        for (i in 0..6) {
            calendar.add(Calendar.DAY_OF_MONTH, i)
            list.add(StatusBean(calendar.time, if (i == 0) true else false))
            calendar = Calendar.getInstance()
        }

        preDateAdapter.addData(list)
        binding.parkpreRv.adapter = preDateAdapter
        binding.parkpreRv.layoutManager = GridLayoutManager(this, 7)

        preDateAdapter.setOnItemClickListener { adapter, view, position ->
            preDateAdapter.data.forEach {
                it.checked = false
            }
            preDateAdapter.data.get(position).checked = true
            preDateAdapter.notifyDataSetChanged()
        }

        binding.parkpreRg.setOnCheckedChangeListener { group, checkedId ->
            binding.parkpreMorning.setTextColor(getColor(com.frxuexi.lib_base.R.color.darkgray))
            binding.parkpreAfter.setTextColor(getColor(com.frxuexi.lib_base.R.color.darkgray))
            binding.parkpreEvening.setTextColor(getColor(com.frxuexi.lib_base.R.color.darkgray))
            when (checkedId) {
                R.id.parkpre_morning -> {
                    binding.parkpreMorning.setTextColor(getColor(R.color.white))
                    binding.parkpreTime.setText("上午 9:00-12:00")
                }

                R.id.parkpre_after -> {
                    binding.parkpreAfter.setTextColor(getColor(R.color.white))
                    binding.parkpreTime.setText("下午 14:00-16:00")
                }

                R.id.parkpre_evening -> {
                    binding.parkpreEvening.setTextColor(getColor(R.color.white))
                    binding.parkpreTime.setText("晚上 18:00-22:00")
                }
            }
        }

        binding.parkpreSave.setOnClickListener {
            val carNo = binding.parkpreCarNo.text.toString()
            val carNum = binding.parkpreCarNum.text.toString()
            val parkEntity = ParkEntity(null, carNo, carNum, SPUtils.getInstance().getString(Const.UNAME),
                DateTimeUtils.getDate(preDateAdapter.data.filter { it.checked }.get(0).content as Date),
                binding.parkpreTime.text.toString().substring(0,2))
            if(id!=-1){
                parkEntity.id=id!!.toLong()
                vm.sendIntent(ParkPreIntent.updatePark(parkEntity))
            }else {
                vm.sendIntent(ParkPreIntent.addPark(parkEntity))
            }
        }
    }

}